<?php
namespace app\shadmin\controller;

use think\Db;
use think\Request;
use app\user\model\Finance_tixian;
class Index extends \think\Controller
{
    public function _initialize()
    {
        // parent::_initialize();
        if (!session('?aid')) {
            $this->redirect('shadmin/login/index');
        }
    }
    //后台首页
    public function index()
    {
        return view();
    }
    //主框架
    public function main()
    {
        $json['code'] = input('type') ? 200 : 400;
        return view();
    }
    //新闻列表页
    public function newslist()
    {
        $s = input('param.');
        if ($s['key']) {
            $where['Title'] = ['like', '%' . $s['key'] . '%'];
        }
        $where['BusModelID'] = $s['listid'];
        $list = Db::table('news')->where($where)->order('ID desc')->paginate(25);
        // 获取分页显示
        $page = $list->render();
        // 模板变量赋值
        $this->assign('num', $list->total());
        $this->assign('list', $list);
        $this->assign('page', $page);
        // 渲染模板输出
        return view();
    }
    //商品详情
    public function product()
    {    
       


        $s = input('param.');
        $t = Db::table('product_1')->find($s['id']);
        if($t['url']){

         $this->redirect($t['url']);   
        }
       $this->assign('t',$t);

        
        return view();
    }
    public function addproduct(){


        $s = input('param.');


        if(IS_AJAX()){



           if($s['tid']){


          
            Db::table('product_1')->where($where)->update($s);
            $json['code']=200;


           }else{



            
          $s['addtime']=date('Y-m-d H:i:s');
          $s['state']=1;
          $s['shoptype']=1;
          $s['addname']=session('name');
          $s['shop']=idtoval('user_shop',['id'=>$s['bid']],'qymc');


        Db::table('product_1')->strict(false)->insert($s);

        $json['code']=200;




           } 
         
         




        return json($json);

        





        }else{

          $sj=Db::table('user_shop')->where(['state'=>3])->select();  

          $this->assign('sj',$sj);

          if($s['id']){

           $t=Db::table('product_1')->find($s['id']); 
           $this->assign('t',$t);
          }

          $catelist = Db::table('product_cate')->where($where)->order('sort desc')->limit(50)->select();
          $this->assign('catelist', $catelist);   

         return view();

        }

       
    }
    //商品列表
    public function productlist()
    {   

        
        $s = input('param.');
        
        if ($s['key']) {
            $where['name'] = ['like', '%' . $s['key'] . '%'];
        }/*
        if ($s['state']) {
            $where['State'] = $s['state'];
            $this->assign('state', $s['state']);
        }
        $where['ShopType'] = 1;
        if ($s['bid']) {
            $where['BusinessID'] = $s['bid'];
        }*/
         
        if($s['bid']){

            $where['bid']=$s['bid'];
        }
       

        $list = Db::table('product_1')->where($where)->alias('p')->order('id desc')->paginate(25);
        // 获取分页显示
        $page = $list->render();
        // 模板变量赋值
       
        $this->assign('list', $list);
        $this->assign('page', $page);
        return view();
    }
    //社区商品列表
    public function sproductlist()
    {
        $s = input('param.');
        if ($s['key']) {
            $where['Name|Business'] = ['like', '%' . $s['key'] . '%'];
        }
        if ($s['state']) {
            $where['State'] = $s['state'];
        }
        $where['ShopType'] = 2;
        if ($s['bid']) {
            $where['BusinessID'] = $s['bid'];
        }
        $list = Db::table('product')->alias('p')->join('unit u', 'p.Unit = u.unid')->where($where)->order('ID desc')->paginate(20);
        // 获取分页显示
        $page = $list->render();
        // 模板变量赋值
        $this->assign('num', $list->total());
        $this->assign('list', $list);
        $this->assign('page', $page);
        return view();
    }
    //商家列表
    public function businesslist()
    {
      
        $list = Db::table('user_shop')->where($where)->order('id desc')->paginate(20);
        // 获取分页显示
        $page = $list->render();
        // 模板变量赋值
        $this->assign('num', $list->total());
        $this->assign('list', $list);
        $this->assign('page', $page);
        return view();
    }
    public function sbusinesslist()
    {
        $where['ShopType'] = 2;
        $list = Db::table('product_business')->where($where)->order('ID desc')->paginate(20);
        // 获取分页显示
        $page = $list->render();
        // 模板变量赋值
        $this->assign('num', $list->total());
        $this->assign('list', $list);
        $this->assign('page', $page);
        return view();
    }
    //买家详情页
    public function business()
    {
        $t = Db::table('user_shop')->find(input('id'));
        $this->assign('t', $t);
        return view();
    }

    public function editbusiness()
    {
        $s = input('param.');
        $t = Db::table('user_shop')->find($s['id']);
        
        $this->assign('t',$t);

        
        return view();
    }
    public function ajaxnews()
    {
    }
    //新闻详情页
    public function news()
    {
        $s = input('param.');




        if ($s['id']) {
            $t = Db::table('news')->find($s['id']);
            $this->assign('t', $t);
        }
        $list = Db::table('news_cate')->where(['ParentID'=>0,'Level'=>1])->select();
        $this->assign('catelist', $list);
        return view();
    }
    public function checknews()
    {
        if (IS_AJAX()) {

            $s = input('param.');


            if ($s['tid']) {

                //编辑新闻
            $s['BusModel']=idtoval('news_cate',['ID'=>$s['BusModelID']],'CategoryName');
            Db::table('news')->where(['ID'=>$s['tid']])->update($s);
            $json['code']=200;



            } else {

                //新增新闻
             $s['addtime']=date('Y-m-d H:i:s');
             $s['BusModel']=idtoval('news_cate',['ID'=>$s['BusModelID']],'CategoryName');
             $s['AccountID']=session('aid');
             $s['AccountName']=session('name');
             $s['State']=1;
              Db::table('news')->insert($s);

            $json['code']=200;


            }
            return json($json);
        }
    }
    public function sells()
    {
    }
    public function orderlist()
    {
        return view();
    }


   //小时购列表 
    public function hourlist()
    {
        $list = Db::table('so9_hour')->where($where)->order('id desc')->select();
        $this->assign('list', $list);
        return view();
    }
    //小时购详情
    public function hour()
    {
        $s = input('param.');
        if (IS_AJAX()) {
            $s['starttime'] = strtotime($s['starttime']);
            $s['overtime'] = strtotime($s['overtime']);
            if ($s['tid']) {
                Db::table('so9_hour')->strict(false)->where(['id' => $s['tid']])->update($s);
                $json['code'] = 200;
            } else {
                Db::table('so9_hour')->strict(false)->insert($s);
                $json['code'] = 200;
            }
            return json($json);
        } else {
            if ($s['id']) {
                $t = Db::table('so9_hour')->find($s['id']);
                $this->assign('t', $t);
            }
            return view();
        }
    }


    //tg
    public function togetherlist()
    {
        $list = Db::table('so9_together')->where($where)->order('id desc')->select();
        $this->assign('list', $list);
        return view();
    }
    //tg
    public function together()
    {
        $s = input('param.');
        if (IS_AJAX()) {
            $s['starttime'] = strtotime($s['starttime']);
            $s['overtime'] = strtotime($s['overtime']);
            if ($s['tid']) {
                Db::table('so9_together')->strict(false)->where(['id' => $s['tid']])->update($s);
                $json['code'] = 200;
            } else {
                Db::table('so9_together')->strict(false)->insert($s);
                $json['code'] = 200;
            }
            return json($json);
        } else {
            if ($s['id']) {
                $t = Db::table('so9_together')->find($s['id']);
                $this->assign('t', $t);
            }
            return view();
        }
    }





    //秒杀列表
    public function seckilllist()
    {
        $list = Db::table('so9_seckill')->where($where)->order('id desc')->select();
        $this->assign('list', $list);
        return view();
    }
    //秒杀详情
    public function seckill()
    {
        $s = input('param.');
        if (IS_AJAX()) {
            $s['starttime'] = strtotime($s['starttime']);
            $s['overtime'] = strtotime($s['overtime']);
            if ($s['tid']) {
                Db::table('so9_seckill')->strict(false)->where(['id' => $s['tid']])->update($s);
                $json['code'] = 200;
            } else {
                Db::table('so9_seckill')->strict(false)->insert($s);
                $json['code'] = 200;
            }
            return json($json);
        } else {
            if ($s['id']) {
                $t = Db::table('so9_seckill')->find($s['id']);
                $this->assign('t', $t);
            }
            return view();
        }
    }

    //拼单
    public function gatherlist()
    {
        $list = Db::table('so9_gather')->where($where)->order('id desc')->select();
        $this->assign('list', $list);
        return view();
    }
    //拼单列表
    public function gather()
    {
        $s = input('param.');
        if (IS_AJAX()) {
            $s['starttime'] = strtotime($s['starttime']);
            $s['overtime'] = strtotime($s['overtime']);
            if ($s['tid']) {
                Db::table('so9_gather')->strict(false)->where(['id' => $s['tid']])->update($s);
                $json['code'] = 200;
            } else {
                Db::table('so9_gather')->strict(false)->insert($s);
                $json['code'] = 200;
            }
            return json($json);
        } else {
            if ($s['id']) {
                $t = Db::table('so9_gather')->find($s['id']);
                $this->assign('t', $t);
            }
            return view();
        }
    }


    //积分商城

    public function jifenlist(){

     $where['type']=input('type');
        $list = Db::table('jifen_product')->where($where)->alias('p')->order('id desc')->paginate(25);
        // 获取分页显示
        $page = $list->render();
        // 模板变量赋值

        $this->assign('list', $list);
        $this->assign('page', $page);
        return view();
    }



    //发布产品
    public function jifenproduct(){
        $s = input('param.');
        if(IS_AJAX()){

            if($s['tid']){

                Db::table('jifen_product')->where('id',$s['tid'])->update($s);

            }else{

                $s['addtime']=date('Y-m-d H:i:s');
                $s['state']=1;
                $s['addname']=session('name');
                Db::table('jifen_product')->strict(false)->insert($s);


            }








            $json['code']=200;
            return json($json);

        }else{

          if($s['id']){

            $fg= Db::table('jifen_product')->find($s['id']);
            $this->assign('t',$fg);
          }


            return view();

        }


    }


 
 public function userlist(){
       if(input('key')){
         $key=input('key');
          $where['name|nickname|tel']=['like',"%".$key."%"];

       }
    
        $list = Db::table('user')->where($where)->alias('p')->order('id desc')->paginate(25);
        // 获取分页显示
        $page = $list->render();
        // 模板变量赋值
        $all=Db::table('user')->count();
        $this->assign('all',$all);
        $this->assign('list', $list);
        $this->assign('page', $page);
        return view();
    }





    public function product99list(){


        $list = Db::table('so9_99product')->where($where)->alias('p')->order('id desc')->paginate(25);
        // 获取分页显示
        $page = $list->render();
        // 模板变量赋值

        $this->assign('list', $list);
        $this->assign('page', $page);
        return view();
    }



    //发布产品
    public function product99(){
        $s = input('param.');
        if(IS_AJAX()){

            if($s['tid']){

                Db::table('so9_99product')->where('id',$s['tid'])->update($s);

            }else{

                $s['addtime']=date('Y-m-d H:i:s');
                $s['state']=1;
                $s['addname']=session('name');
                Db::table('so9_99product')->strict(false)->insert($s);


            }








            $json['code']=200;
            return json($json);

        }else{

            if($s['id']){

                $fg= Db::table('so9_99product')->find($s['id']);
                $this->assign('t',$fg);

            }


            return view();

        }


    }


 //规格列表
    public function gglist(){
    
      $where['pid']=input('id');
      $list=Db::table('product_price')->where($where)->select();
      $this->assign('list',$list);

       return view();


    }
 //产品规格
    public function gg(){




        $s = input('param.');
        unset($s['id']);

        if(IS_AJAX()){

            if($s['tid']){

                Db::table('product_price')->where('id',$s['tid'])->update($s);

            }else{

                
                Db::table('product_price')->strict(false)->insert($s);


            }








            $json['code']=200;
            return json($json);

        }else{

            if($s['id']){

                $fg= Db::table('product_price')->find($s['id']);
                $this->assign('t',$fg);

            }


            return view();

        }

    }


  //修改密码
   public function changepwd(){
 
    if(IS_AJAX()){
     
    $s=input('param.');
    if(!$s['p1'] || $s['p2']!=$s['p3']){

        $json['msg']="资料输入不正确";
        $json['code']=400;

    }else{

        $where['id']=session('aid');
        $where['pwd']=md5($s['p1'].'sohe2017');
        $user=Db::table('sys_admin')->where($where)->find();
        if($user){
             $data['pwd']=md5($s['p2'].'sohe2017');
             Db::table('sys_admin')->where($where)->update($data);
             $json['code']=200;

        }else{
          

            $json['code']=400;
            $json['msg']="您输入的旧密码错误";

        }

    } 
    
   return json($json);

    }else{

      return view();   
    }

   
   }
   //后台商家申请提现
   public function cashwithdrawal(Finance_tixian $Finance_tixian)
   {
        $info = $Finance_tixian->select();
        $this->assign('info',$info);
        return $this->fetch();
   }


}